package com.liuweiheng.cloud.config.mq;

import com.liuweiheng.base.constant.mq.GlobalExchangeConstant;
import com.liuweiheng.base.constant.mq.GlobalQueueConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Rabbitmq-Direct模式 配置类
 * @author lwh
 * @since 2021/5/31
 */
@Configuration
public class DirectRaabitConfig {
    /**
     * 注册消息队列-TestDirectQueue
     * durable: 是否开启持久化，默认不开启，持久化队列：消息会被持久化到硬盘上，当服务器重启时，任然有效
     * exclusive: 默认为false, true表示队列只能被当前连接使用，其他连接无法使用该队列，且当前连接关闭后，队列删除
     * autoDelete: 是否自动删除，当没有生产者和消费者使用该队列时，删除该队列
     *
     */
    @Bean
    public Queue TestDirectQueue() {
        return new Queue(GlobalQueueConstant.TEST_QUEUE, true);
    }

    /**
     * 注册交换机 - TestDirectExchange
     */
    @Bean
    public DirectExchange TestDirectExchange() {
        return new DirectExchange(GlobalExchangeConstant.TEST_DIRECT, true, false);
    }

    /**
     * 绑定队列到交换机，队列-TestDirectQueue，交换机-TestDirectExchange, routingKey-TestDirectRouting
     */
    @Bean
    public Binding BindingDirect(@Qualifier("TestDirectQueue") Queue queue,
                                 @Qualifier("TestDirectExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(GlobalQueueConstant.TEST_KEY);
    }

    @Bean
    public DirectExchange AnotherDirectExchange() {
        return new DirectExchange("AnotherDirectExchange");
    }
}
